/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
// 暴力破解法
var getIntersectionNode = function (headA, headB) {
  let myA = headA;
  while (myA) {
    let myB = headB;
    while (myB) {
      if (myA === myB) return myA;
      myB = myB.next;
    }
    myA = myA.next;
  }
  return null;
};

// 哈希法
var getIntersectionNode = function (headA, headB) {
  if (!headA || !headB) return null;

  let hashMap = new Map();
  let pA = headA;
  while (pA) {
    hashMap.set(pA, 1);
    pA = pA.next;
  }

  let pB = headB;
  while (pB) {
    if (hashMap.has(pB)) return pB;
    pB = pB.next;
  }
};
// 哈希法，使用set集合对象
var getIntersectionNode = function (headA, headB) {
  if (!headA || !headB) return null;

  let myset = new Set();
  let pA = headA;
  while (pA) {
    myset.add(pA);
    pA = pA.next;
  }

  let pB = headB;
  while (pB) {
    if (myset.has(pB)) return pB;
    pB = pB.next;
  }
};
